let count = 0;
let editingId = null;
let requestUrl = null;

let elements = [];
let typeArray = ["野生", "宝宝", "变异"];
let tags = ["", "bind", "bidai", "", "lw"];
let sourceArray = ["#chongwu_list", "#chongwu_datalist"];
let zizhiTypeArray = ["&emsp;&emsp;野生&emsp;&emsp;", "&emsp;&emsp;宝宝&emsp;&emsp;", "&emsp;&emsp;变异&emsp;&emsp;"];
/**
 * 资质范围数组
 */
let rangeArray = [{
	min: 7 / 8,
	max: 1,
	from: 12,
	to: 0
}, {
	min: 0.8,
	max: 1,
	from: 48,
	to: 0
}, {
	min: 0.8,
	max: 1,
	from: 48,
	to: 0
}];
let jsonData = {
	"code": 200,
	"data": [{
		"value": "all",
		"text": "全部",
		"url": "/chongwu_category/selectAll"
	}, {
		"value": "0-65",
		"text": "0-65级",
		"url": "/chongwu_category/selectByGradeRange?min=0&max=65"
	}, {
		"value": "75-85",
		"text": "75-85级",
		"url": "/chongwu_category/selectByGradeRange?min=75&max=85"
	}, {
		"value": "90-115",
		"text": "90-115级",
		"url": "/chongwu_category/selectByGradeRange?min=90&max=115"
	}, {
		"value": "special",
		"text": "特殊",
		"url": "/chongwu_category/selectSpecial"
	}],
	"message": "请求成功"
};

function addHandler() {
	requestUrl = "/chongwu_category/insert";

	$("#chongwu_dialog").dialog("open");
}

function editHandler() {
	let rowData = $("#chongwu_list").datagrid("getSelected");
	
	if(rowData) {
		requestUrl = "/chongwu_category/updateById";

		$("#id").val(rowData.id);
		$("#name").textbox("setValue", rowData.name);
		$("#type").combobox("setValue", rowData.type);
		$("#grade").numberspinner("setValue", rowData.grade);
		$("#neidanId").combobox("setValue", rowData.neidanId);
		$("#lifespan").numberspinner("setValue", rowData.lifespan);
		$("#specialType").combobox("setValue", rowData.specialType);
		$("#neidanQuantity").numberspinner("setValue", rowData.neidanQuantity);

		$("#chongwu_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function imageHandler() {
	let rowData = $("#chongwu_list").datagrid("getSelected");

	if(rowData) {
		requestUrl = "/chongwu_category/updateImageById";

		$("#categoryId").val(rowData.id);
		$("#img").attr("src", rowData.image);

		$("#upload_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function zizhiHandler() {
	let rowData = $("#chongwu_list").datagrid("getSelected");

	if(rowData) {
		if (rowData.specialType != null) {
			alertMsg("特殊宠物的资质不需要手动设置");

			return;
		}

		$("#zizhi_form").form("clear");
		$("#chongwu_id").val(rowData.id);

		ajaxGet("/chongwu_init_zizhi/selectList", {
			type: 1,
			chongwuId: rowData.id
		}, function(resp) {
			let list = resp.data;

			if(list && list.length === 1) {
				let data = list[0];

				$("#gjzz").numberspinner("setValue", data.gjzz);
				$("#fyzz").numberspinner("setValue", data.fyzz);
				$("#tlzz").numberspinner("setValue", data.tlzz);
				$("#flzz").numberspinner("setValue", data.flzz);
				$("#sdzz").numberspinner("setValue", data.sdzz);
				$("#chengzhang").numberspinner("setValue", data.chengzhang);

				// 特殊宠物才有的字段
				$("#zizhi_name").textbox("setValue", data.name);
				$("#special_type").textbox("setValue", data.specialType);
				$("#fight_grade").textbox("setValue", data.fightGrade);
				$("#improve_times").textbox("setValue", data.improveTimes);
				$("#skill_quantity").textbox("setValue", data.skillQuantity);
				$("#neidan_quantity").textbox("setValue", data.neidanQuantity);
			}
		}, error);

		ajaxGet("/chongwu_category/selectById", {
			id: rowData.id
		}, function (resp) {
			let data = resp.data;

			if (data.specialType) {
				requestUrl = "/chongwu_init_zizhi/saveSpecial";
			} else {
				requestUrl = "/chongwu_init_zizhi/save";
			}

			if (rowData.specialType) {
				$("#special_type").combobox("setValue", rowData.specialType);
			}

			$("#zizhi_dialog").dialog("open");
		}, error);

		$("#zizhi_dialog").dialog("open");
	} else {
		unselected();
	}
}

function skillHandler() {
	let rowData = $("#chongwu_list").datagrid("getSelected");

	if(rowData) {
		$("#skill_list").datagrid("reload", {
			chongwuId: rowData.id
		});

		$("#skill_dialog").dialog("open");
	} else {
		unselected();
	}
}

function viewHandler() {
	let rowData = $("#chongwu_list").datagrid("getSelected");

	if(rowData) {
		elements = [];

		$("#condition").combobox("setValue", "all");

		$("#chongwu_datalist").datalist("reload", {
			fallback: function () {
				$("#chongwu_datalist").datalist("selectRecord", rowData.id);
			}
		});

		$("#tujian_dialog").dialog("open");
	} else {
		unselected();
	}
}

/**
 * 加载宠物技能
 * @param selector 元素的ID属性值
 * @param source 数据来源
 */
function loadSkills(selector, source) {
	let rowData = $(sourceArray[source]).datagrid("getSelected");

	if(rowData) {
		$("#" + selector).empty();

		ajaxGet("/chongwu_init_skill/selectByChongwuId", {
			chongwuId: rowData.id
		}, function(resp) {
			let chongwu_skills = "";
			let data = resp.data;

			if(data.length > 0) {
				for(let i = 0; i < data.length; i++) {
					let item = data[i];

					let tag = item.tag;
					let name = item.name;
					let type = item.type;
					let image = item.image;
					let verifiable = item.verifiable;

					chongwu_skills += "<td>";

					// 低级技能，不可认证：蓝技能框
					if(type === 1 && verifiable === 0) {
						chongwu_skills += "<div class='skill_box skill_blue' title='" + name + "'>";

						chongwu_skills += "<img src='" + image + "' />";
					} else {
						// 高级技能和可认证的技能：紫色技能框
						chongwu_skills += "<div class='skill_box skill_purple' title='" + name + "'>";

						chongwu_skills += "<img src='" + image + "' />";
					}

					if(tag !== 3) {
						chongwu_skills += "<i class='" + tags[tag] + "'></i>";
					}

					chongwu_skills += "</div>";
					chongwu_skills += "</td>";

					elements.push(chongwu_skills);

					chongwu_skills = "";
				}

				ajaxGet("/chongwu_taozhuang_skill_category/selectByChongwuId", {
					chongwuId: rowData.id
				}, function(response) {
					let result = response.data;

					if (result) {
						chongwu_skills += "<td>";
						chongwu_skills += 	"<div class='taozhuang_skill_box' title='" + result.name + "'>";
						chongwu_skills += 		"<img class='img' src='" + result.image + "' />";
						chongwu_skills += 		"<i></i>";
						chongwu_skills += 	"</div>";
						chongwu_skills += "</td>";

						elements.push(chongwu_skills);

						chongwu_skills = "";
					}

					ajaxGet("/chongwu_neidan_category/selectByChongwuId", {
						chongwuId: rowData.id
					}, function(resp) {
						let result = resp.data;

						if (result) {
							chongwu_skills += "<td>";
							chongwu_skills += 	"<div class='zhuanshu_neidan_box' title='" + result.name + "'>";
							chongwu_skills += 		"<img class='img' src='" + result.image + "' />";
							chongwu_skills += 		"<i></i>";
							chongwu_skills += 	"</div>";
							chongwu_skills += "</td>";

							elements.push(chongwu_skills);

							chongwu_skills = "";
						}

						let skills = "<tr>";
						let ends = false; // 是否收尾
						let length = elements.length;

						for (let i = 1; i <= length; i++) {
							ends = false;
							skills += elements[i - 1];

							if (i % 4 === 0) {
								skills += "</tr>";

								if (i === length) {
									ends = false;
								} else {
									skills += "<tr>";
									ends = true;
								}
							}
						}

						// 收尾
						if (ends) {
							skills += "</tr>";
						}

						$("#" + selector).append(skills);
					}, error);
				}, error);
			}
		}, error);
	} else {
		unselected();
	}
}

/**
 * 关闭所有宠物资质标签
 */
function closeAll() {
	$("#tabs .tabs-inner > span.tabs-title").each(function(i, n) {
		let title = $(n).text();

		$("#tabs").tabs("close", title);
	});
}

/**
 * 加载特殊宠物的初始资质
 * @param specialType 特殊宠物的资质类型
 * @param rowData 宠物信息
 */
function loadSpecial(specialType, rowData) {
	ajaxGet("/chongwu_init_zizhi/selectSpecials", {
		specialType: specialType
	}, function(resp) {
		let list = resp.data;

		if (list && list.length > 0) {
			let lifespan = rowData.lifespan;

			if (lifespan <= 0) {
				if (lifespan < 0) {
					lifespan = "<span class='red'>限时</span>";
				} else {
					lifespan = "<span class='blue'>永生</span>";
				}
			}

			for (let i = 0; i < list.length; i++) {
				let data = list[i];

				let gjzz = data.gjzz;
				let fyzz = data.fyzz;
				let tlzz = data.tlzz;
				let flzz = data.flzz;
				let sdzz = data.sdzz;
				let chengzhang = (data.chengzhang / 1000).toFixed(3)
				let	neidanQuantity = data.neidanQuantity ? data.neidanQuantity : rowData.neidanQuantity;

				let divElement = "";

				divElement += "<table class='f28'>";
				divElement += 	"<tr>";
				divElement += 		"<td>攻击资质</td>";
				divElement += 		"<td>" + gjzz + "</td>";
				divElement += 		"<td>防御资质</td>";
				divElement += 		"<td>" + fyzz + "</td>";
				divElement += 		"</tr>";
				divElement += 	"<tr>";

				divElement += 	"<td>体力资质</td>";
				divElement += 		"<td>" + tlzz + "</td>";
				divElement += 		"<td>法力资质</td>";
				divElement += 		"<td>" + flzz + "</td>";
				divElement += 		"</tr>";
				divElement += 	"<tr>";

				divElement += 	"<td>速度资质</td>";
				divElement += 		"<td>" + sdzz + "</td>";
				divElement += 		"<td>成&emsp;&emsp;长</td>";
				divElement += 		"<td>" + chengzhang + "</td>";
				divElement += 	"</tr>";

				divElement += 	"<tr>";
				divElement += 		"<td>寿&emsp;&emsp;命</td>";
				divElement += 		"<td>" + lifespan + "</td>";
				divElement += 		"<td>内丹数量</td>";
				divElement += 		"<td>" + neidanQuantity + "</td>";
				divElement += 	"</tr>";
				divElement += "</table>";

				let title = data.name;

				$("#tabs").tabs("add", {
					title: title,
					content: divElement
				});
			}

			$("#tabs").tabs("showHeader");
		}
	}, error);
}

$(document).ready(function() {

	$("#_type_").combobox({
		url: "/chongwu_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 120,
		prompt: "-宠物类型-",
		panelHeight: "auto"
	});

	$("#_grade_").numberspinner({
		min: 0,
		max: 115,
		width: 120,
		prompt: "参战等级"
	});

	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		let grade = $("#_grade_").numberspinner("getValue");
		let type = $("#_type_").combobox("getValue");

		$("#chongwu_list").datagrid("reload", {
			grade: grade,
			type: type
		});
	});

	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	$("#name").textbox({
		width: 128,
		required: true
	});

	$("#type").combobox({
		url: "/chongwu_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 128,
		required: true,
		panelHeight: "auto"
	});

	$("#grade").numberspinner({
		min: 0,
		max: 115,
		width: 128,
        required: true
	});

	$("#lifespan").numberspinner({
        min: -1,
		max: 12000,
        width: 128,
        editable: true
    });

	$("#neidanQuantity").numberspinner({
		min: 3,
		max: 5,
		width: 128,
		editable: true
	});

	$("#specialType").combobox({
		url: "/chongwu_zizhi_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 94,
		panelHeight: "auto"
	});

	$("#tip_specialType").linkbutton({
		iconCls: "icon-help"
	}).click(function () {
		let message = "设置了资质类型的宠物造型不需要额外设置其资质，而是从预先设置的数据中（根据这个字段的值）查询。";

		alertMsg(message);
	});

	$("#neidanId").combobox({
		url: "/chongwu_neidan_category/selectList?type=3",
		valueField: "id",
		textField: "name",
		width: 128,
		required: true,
		panelHeight: "auto",
		formatter: function(row) {
			return "<img width='20' src='" + row.image + "' />&nbsp;" + row.name;
		}
	});

	$("#chongwu_dialog").dialog({
		title: "宠物类型",
		width: 500,
		height: 300,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#chongwu_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#chongwu_dialog").dialog("close");
						$("#chongwu_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#chongwu_form").form("clear");
				$("#chongwu_dialog").dialog("close");
			}
		}]
	});

	/******************************************************************************************************/

	fetchUploadFilebox("#chongwu_list", "/chongwu_category/upload");

	fetchUploadDialog("/chongwu_category/updateImageById", function () {
		$("#chongwu_list").datagrid("reload");
	});

	/******************************************************************************************************/

	fetchSelect("#chongwu_type", typeArray, {
		width: 120,
		prompt: "-宠物类型-",
		onClick: function(record) {
			if (record) {
				let rowData = $("#chongwu_list").datagrid("getSelected");

				if (rowData) {
					let chongwuId = rowData.id;

					$("#zizhi_form").form("clear");
					$("#chongwu_id").val(chongwuId);

					// 查询宠物初始资质
					ajaxGet("/chongwu_init_zizhi/selectList", {
						type: record.value,
						chongwuId: chongwuId
					}, function (resp) {
						let list = resp.data;

						if (list && list.length === 1) {
							let data = list[0];

							$("#gjzz").numberspinner("setValue", data.gjzz);
							$("#fyzz").numberspinner("setValue", data.fyzz);
							$("#tlzz").numberspinner("setValue", data.tlzz);
							$("#flzz").numberspinner("setValue", data.flzz);
							$("#sdzz").numberspinner("setValue", data.sdzz);
							$("#chengzhang").numberspinner("setValue", data.chengzhang);
							// 特殊宠物才有的字段
							$("#zizhi_name").textbox("setValue", data.name);
							$("#special_type").textbox("setValue", data.specialType);
							$("#fight_grade").textbox("setValue", data.fightGrade);
							$("#improve_times").textbox("setValue", data.improveTimes);
							$("#skill_quantity").textbox("setValue", data.skillQuantity);
							$("#neidan_quantity").textbox("setValue", data.neidanQuantity);
						}
					}, error);
				}
			}
		}
	});

	$("#gjzz").numberspinner({
		min: 0,
		width: 120,
		editable: true,
		required: true
	});

	$("#fyzz").numberspinner({
		min: 0,
		width: 120,
		editable: true,
		required: true
	});

	$("#tlzz").numberspinner({
		min: 0,
		width: 120,
		editable: true,
		required: true
	});

	$("#flzz").numberspinner({
		min: 0,
		width: 120,
		editable: true,
		required: true
	});

	$("#sdzz").numberspinner({
		min: 0,
		width: 120,
		editable: true,
		required: true
	});

	$("#chengzhang").numberspinner({
		min: 0,
		max: 1300,
		width: 120,
		required: true
	});

	$("#zizhi_name").textbox({
		width: 120,
		required: false
	});

	$("#special_type").combobox({
		url: "/chongwu_zizhi_type/selectAll",
		valueField: "id",
		textField: "name",
		width: 120,
		editable: true
	});

	$("#fight_grade").numberspinner({
		min: 1,
		max: 115,
		width: 120,
		editable: true
	});

	$("#improve_times").numberspinner({
		min: 1,
		max: 3,
		width: 120,
		editable: true
	});

	$("#skill_quantity").numberspinner({
		min: 1,
		max: 5,
		width: 120,
		editable: true
	});

	$("#neidan_quantity").numberspinner({
		min: 1,
		max: 5,
		width: 120,
		editable: true
	});

	$("#zizhi_dialog").dialog({
		title: "初始资质",
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#zizhi_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#zizhi_dialog").dialog("close");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#zizhi_form").form("clear");
				$("#zizhi_dialog").dialog("close");
			}
		}]
	});

	/******************************************************************************************************/

	/**
	 * tujian_dialog
	 */
	$("#condition").combobox({
		data: jsonData,
		valueField: "value",
		textField: "text",
		width: 120,
		panelHeight: "auto",
		onSelect: function (row) {
			if (row) {
				$("#chongwu_datalist").datalist("reload", row.url);
			}
		}
	});

	$("#chongwu_datalist").datalist({
		url: "/chongwu_category/selectAll",
		idField: "id",
		valueField: "id",
		textField: "name",
		width: 198,
		height: 370,
		textFormatter: function(value, rowData, rowIndex) {
			let content = "";

			content += "<table>";
			content += 	"<tr>";
			content += 		"<td rowspan='2'>";
			content += 			"<img width='50' src='" + rowData.image + "' />";
			content += 		"</td>";
			content += 		"<td>" + rowData.name + "</td>";
			content += 	"</tr>";
			content += 	"<tr>";
			content += 		"<td>参战等级：" + rowData.grade + "级</td>";
			content += 	"</tr>";
			content += "</table>";

			return content;
		},
		onSelect: function(rowIndex, rowData) {
			if(rowData) {
				elements = [];

				$("#chongwu_name").html(rowData.name);
				$("#chongwu_grade").html(rowData.grade);
				$("#chongwu_image").attr("src", rowData.image);

				$("#tabs").tabs("hideHeader");

				closeAll();

				if (rowData.specialType) {
					loadSpecial(rowData.specialType, rowData);
				} else {
					let chongwuId = rowData.id;

					ajaxGet("/chongwu_init_zizhi/selectList", {
						chongwuId: chongwuId
					}, function(resp) {
						let list = resp.data;

						if(list && list.length > 0) {
							for (let i = 0; i < list.length; i++) {
								let data = list[i];

								let type = data.type;
								let gjzz = data.gjzz;
								let fyzz = data.fyzz;
								let tlzz = data.tlzz;
								let flzz = data.flzz;
								let sdzz = data.sdzz;
								let min = rangeArray[type].min;
								let max = rangeArray[type].max;
								let from = rangeArray[type].from;
								let to = rangeArray[type].to;
								let chengzhang = data.chengzhang;
								let neidanQuantity = data.neidanQuantity ? data.neidanQuantity : rowData.neidanQuantity;

								let lifespan = rowData.lifespan;

								if(lifespan <= 0) {
									if(lifespan < 0) {
										lifespan = "<span class='red'>限时</span>";
									} else {
										lifespan = "<span class='blue'>永生</span>";
									}
								}

								let divElement = "";

								divElement += "<table class='f20'>";
								divElement += 	"<tr>";
								divElement += 		"<td>攻击资质</td>";
								divElement += 		"<td>" + parseInt(gjzz * min) + "-" + parseInt(gjzz * max) + "</td>";
								divElement += 		"<td>防御资质</td>";
								divElement += 		"<td>" + parseInt(fyzz * min) + "-" + parseInt(fyzz * max) + "</td>";
								divElement += 	"</tr>";

								divElement += 	"<tr>";
								divElement += 		"<td>体力资质</td>";
								divElement += 		"<td>" + parseInt(tlzz * min) + "-" + parseInt(tlzz * max) + "</td>";
								divElement += 		"<td>法力资质</td>";
								divElement += 		"<td>" + parseInt(flzz * min) + "-" + parseInt(flzz * max) + "</td>";
								divElement += 	"</tr>";

								divElement += 	"<tr>";
								divElement += 		"<td>速度资质</td>";
								divElement += 		"<td>" + parseInt(sdzz * min) + "-" + parseInt(sdzz * max) + "</td>";
								divElement += 		"<td>成&emsp;&emsp;长</td>";
								divElement += 		"<td>" + ((chengzhang - from) / 1000).toFixed(3) + "-" + ((chengzhang - to) / 1000).toFixed(3) + "</td>";
								divElement += 	"</tr>";

								divElement += 	"<tr>";
								divElement += 		"<td>寿&emsp;&emsp;命</td>";
								divElement += 		"<td>" + lifespan + "</td>";
								divElement += 		"<td>内丹数量</td>";
								divElement += 		"<td>" + neidanQuantity + "</td>";
								divElement += 	"</tr>";
								divElement += "</table>";

								let title = zizhiTypeArray[data.type];

								$("#tabs").tabs("add", {
									title: title,
									content: divElement
								});
							}

							$("#tabs").tabs("showHeader");
						}
					}, error);
				}

				loadSkills("chongwu_skills", 1);
			}
		}
	});

	$("#tabs").tabs({
		fit: true,
		pill: true,
		narrow: false,
		border: false,
		closable: false,
		justified: true,
		showHeader: true,
		onSelect: function (title, index) {
			let chongwu = $("#chongwu_datalist").datalist("getSelected");

			let specialType = chongwu.specialType;

			if (chongwu && specialType && index=== 7 && title === "成年") {
				count ++;

				if (count % 2 === 0) {
					$("#tabs").tabs("hideHeader");

					closeAll();

					loadSpecial(specialType + 1, chongwu);
				}
			}
		}
	});

	$("#tujian_dialog").dialog({
		title: "宠物图鉴"
	});

	/******************************************************************************************************/

	$("#skill_list").datagrid({
		url: "/chongwu_init_skill/selectList",
		method: "get",
		width: 540,
		height: 360,
		rownumbers: true,
		fitColumns: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				let row = $("#chongwu_list").datagrid("getSelected");

				if(row) {
					let datagrid = $("#skill_list");

					datagrid.datagrid("appendRow",{
						id: null,
						tag: null,
						skillId: null,
						chongwuId: row.id,
						improveTimes: null
					});

					// 得到表格数据总行数
					let total = datagrid.datagrid("getRows").length;

					editingId = total - 1;

					datagrid.datagrid("beginEdit", editingId);
				} else {
					unselected("请选择一件装备~");
				}
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				let datagrid = $("#skill_list");

				if (editingId != null) {
					confirmAction("上一次修改的数据未保存，是否现在保存？", function () {
						datagrid.datagrid("endEdit", editingId);
					}, function () {
						datagrid.datagrid("selectRow", editingId);
					});
				} else {
					let row = datagrid.datagrid("getSelected");

					if (row) {
						// 获取行索引，这个索引从0开始
						let rowIndex = datagrid.datagrid("getRowIndex", row);

						editingId = rowIndex;

						datagrid.datagrid("beginEdit", rowIndex);
					} else {
						unselected();
					}
				}
			}
		}, "-", {
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				if (editingId != null) {
					// 结束编辑
					$("#skill_list").datagrid("endEdit", editingId);

					editingId = null;
				}
			}
		}, "-", {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				if (editingId != null) {
					$("#skill_list").datagrid("cancelEdit", editingId);

					editingId = null;
				}
			}
		}, "-", {
			iconCls: "icon-delete",
			text: "删除",
			handler: function() {
				let datagrid = $("#skill_list");
				let row = datagrid.datagrid("getSelected");

				if(row) {
					$.messager.confirm("提示", "是否确认删除？", function(bool) {
						if (bool) {
							let index = datagrid.datagrid("getRowIndex", row);

							if (index) {
								datagrid.datagrid("deleteRow", index);
							}
						}
					});
				} else {
					alertMsg("请选择一条记录！", "warning");
				}
			}
		}],
		columns: [[
			{field: "id", hidden: true},
			{field: "chongwuId", hidden: true},
			{field: "skillId", title: "技能", align: "center", width: 100
				, editor: {
					type: "combobox",
					options: {
						url: "/chongwu_skill_category/selectAll",
						valueField: "id",
						textField: "name",
						panelHeight: "auto",
						formatter: function(row) {
							return "<img height='20' src='" + row.image + "' />&nbsp;" + row.name;
						}
					}
				}
				, formatter: function(value, rowData, rowIndex) {
					if (value) {
						ajaxGet("/chongwu_skill_category/selectById", {
							id: value
						}, function (resp) {
							let data = resp.data;

							$("#skill_" + rowIndex).attr({
								"alt": data.name,
								"src": data.image,
								"title": data.name
							});
						}, error);

						return "<img height='" + size + "' id='skill_" + rowIndex + "'/>";
					}
				}
			},
			{field: "tag", title: "标签", align: "center", width: 100
				, editor: {
					type: "combobox",
					options: {
						url: "/chongwu_skill_tag/selectAll",
						valueField: "id",
						textField: "name",
						panelHeight: "auto"
					}
				}
				, formatter: function(value, rowData, rowIndex) {
					if (value) {
						ajaxGet("/chongwu_skill_tag/selectById", {
							id: value
						}, function (resp) {
							let data = resp.data;

							$("#skill_tag_" + rowIndex).html(data.name);
						}, error);

						return "<div  id='skill_tag_" + rowIndex + "'/>";
					}
				}
			},
			{field: "improveTimes", title: "提升次数", align: "center", width: 100
				, editor: {
					type: "numberspinner",
					options: {
						min: 1,
						max: 3
					}
				}
				, formatter: function(value) {
					if (value && value > 0) {
						return value + "次";
					} else {
						return "/";
					}
				}
			}
		]]
	});

	$("#skill_dialog").dialog({
		title: "配置技能",
		modal: true,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let rows = $("#skill_list").datagrid("getRows");

				ajaxPost("/chongwu_init_skill/save", {
					list: JSON.stringify(rows)
				}, function(resp) {
					showMsg(resp.message);

					$("#skill_dialog").dialog("close");
				}, error);
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#skill_dialog").dialog("close");
			}
		}]
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_image * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#chongwu_list").datagrid({
		url: "/chongwu_category/selectByPage",
		method: "get",
		height: height_table,
		fitColumns: true,
		rownumbers: true,
		pagination: true,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", {
			iconCls: "icon-upload",
			text: "修改图片",
			handler: function() {
				imageHandler();
			}
		}, "-", {
			iconCls: "icon-set",
			text: "设置资质",
			handler: function () {
				zizhiHandler();
			}
		}, "-", {
			iconCls: "icon-shield",
			text: "设置技能",
			handler: function () {
				skillHandler();
			}
		}, "-", {
			iconCls: "icon-search",
			text: "查看详情",
			handler: function () {
				viewHandler();
			}
		}],
		columns: [[
			{field: "id", title: "编号", align: "center", hidden: true},
			{field: "name", title: "宠物名称", align: "center", width: 100},
			{field: "image", title: "宠物头像", align: "center", width: 50
				, formatter: function(value) {
					if (value) {
						return "<img height='" + size + "' src='" + value + "' />";
					} else {
						return "/";
					}
				}
			},
			{field: "type", title: "宠物类型", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/chongwu_type/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#type_" + rowIndex).html(data.name);
					}, error);

					return "<div class='blue' id='type_" + rowIndex + "'></div>";
				}
			},
			{field: "grade", title: "参战等级", align: "center", width: 50},
			{field: "lifespan", title: "初始寿命", align: "center", width: 100
				, formatter: function(value) {
					if(value === -1) {
						return "<div class='red'>限时</div>";
					} else if(value === 0) {
						return "<div class='blue'>永生</div>";
					} else {
						return "<div>" + value + "</div>";
					}
				}
			},
			{field: "neidanId", title: "专属内丹", align: "center", width: 100
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/chongwu_neidan_category/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#neidan_" + rowIndex).attr({
							"alt": data.name,
							"src": data.image,
							"title": data.name
						});
					}, error);

					return "<img height='" + size + "' id='neidan_" + rowIndex + "'/>";
				}
			}
		]]
	});

});